<%-- 
    Document   : admin_createProduct
    Created on : Mar 22, 2014, 11:28:14 AM
    Author     : Dell
--%>

<%@page import="pojo.Colors"%>
<%@page import="pojo.Images"%>
<%@page import="pojo.Sizes"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="pojo.Category"%>
<%@page import="pojo.Product"%>
<%@page import="dao.ProductJpaController"%>
<%@page import="javax.persistence.Persistence"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:include page="admin_header.jsp" />

<style>
#ftable
{
background-color:#fff;
text-align:center;
margin: 0 auto;
margin-top:5px;
width:300px;
border-collapse:collapse;
}
#ftable tr  td
{
padding:5px;


}
#ftable tr td
{
    min-width: 200px;
}
#ftable th
{
    font-size: 15px;
}

</style>
<!-- body -->
<div class='topblock'>

    <center>Edit product</center>
	
</div>

<div class='pageblock'>	
	
    <center>
    
            <form action="myUpload2" method="POST" enctype="multipart/form-data">
                
        <table id="ftable">
            
            <% 
                String pid = request.getParameter("pid");
                
            ProductJpaController p = new ProductJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
            
            Product product = new Product();
            product = p.findProduct(new Integer(pid));
            
            Category category = product.getCategoryId();
            
            List<Sizes> sizelist = new ArrayList();
            sizelist = product.getSizesList();
            
            List<Colors> colorlist = new ArrayList();
            colorlist = product.getColorsList();
            
            List<Images> imglist = new ArrayList();
            imglist = product.getImagesList();
            
            String kids;
            if(category.getKids() == 1)
                kids = "Kids";
            else
                kids = "Adults";
            %>
            
            <input type="hidden" value="<%=product.getId()%>" name="id"/>
            <tr> <td style="text-align:right;">Category</td> <td> <select name="category"><option value="<%=category.getId()%>"><%=category.getDescription()+" "+category.getGender()+" "+kids%></option></select> </td> <td id="req1" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Name</td> <td><input type="text" name="name" id="name" value="<%=product.getName()%>"/></td> <td id="req2" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Price</td> <td><input type="text" name="price" id="price" value="<%=product.getPrice()%>" /></td> <td id="req3" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Amount</td> <td><input type="text" name="amount" id="amount" value="<%=product.getAmount()%>"/></td> <td id="req4" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Brand</td> <td><input type="text" name="brand" id="brand" value="<%=product.getBrand()%>"/></td> <td id="req5" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Description</td> <td><input type="text" name="desc" id="desc" value="<%=product.getDescription()%>"/></td> <td id="req6" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Sizes</td> 
                
                <td>    
                    <% 
                    String tester="";
                        for(Sizes sii : sizelist)
                        {
                         if(sii.getSize().equals("S")) 
                             tester +="S";
                         
                         if(sii.getSize().equals("M"))
                            tester +="M";
                         
                         if(sii.getSize().equals("L"))
                            tester +="L";
                        }
                        
                        if(tester.contains("S"))
                           out.println("<input type='checkbox' name='size1' value='S' checked />Small &nbsp;"); 
                        else
                            out.println("<input type='checkbox' name='size1' value='S' />Small &nbsp;"); 
                        
                        if(tester.contains("M"))
                           out.println("<input type='checkbox' name='size2' value='M' checked />Medium &nbsp;"); 
                        else
                            out.println("<input type='checkbox' name='size2' value='M' />Medium &nbsp;"); 
                        
                        if(tester.contains("L")) 
                           out.println("<input type='checkbox' name='size3' value='L' checked />Large "); 
                        else
                            out.println("<input type='checkbox' name='size3' value='L' />Large "); 
                    %>
                </td> <td id="req7" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Colors</td> 
                
                <td>
                    
                    <%
                        String fullcolor = "";
                        int t1 = 1;
                        for(Colors colors : colorlist)
                        {
                            if(t1 == 1)
                            {
                                fullcolor += colors.getColor();
                                t1 = 0;
                            }
                            else
                            fullcolor += ","+colors.getColor();
                        }
                    %>
                    
                    <input type="text" name="color" id="color" value="<%=fullcolor%>" placeholder="Red, Green, Blue, etc" />
                    
                </td><td id="req8" style="color: red;"></td> </tr>
            
            <tr> <td style="text-align:right;">Image</td> <td style="text-align:right;">
                    
                    
                    
                    <input type="file" name="image" id="image" />
                    
                </td> 
                
                
                <td id="req9" style="color: red;"></td> </tr>
            
            <tr><td></td><td><% 
                        for(Images isa :imglist)
                        {out.println("<div id='my_popup' style='display:none;border:1px dotted gray;padding:.3em;position:fixed; width:600px; height:320px; left:50%; top:50%; z-index:999; margin-left:-300px; margin-top:-160px;'><img src='"+isa.getLocation()+"'alt='' height='320px' width='600px'></img></div>");
                            out.print("<a href='"+isa.getLocation()+"' onClick=\" $('#dim_wrapper').toggle(); $('#dim_wrapper').animate({'opacity':0.6}, 700 ); show_popup('my_popup');  return false;\" > View current</a>");
                        }
                    %></td><td></td></tr>
            
        </table>
    
                <br/>
                <input type="submit" name="submit" id="submit" value="Edit" />
                </form>
    </center>
    
</div>
                    
<div id='dim_wrapper' style='
	width:100%;
	height:100%;
	position:fixed;
	top:0;
	left:0;
	z-index:998;
	background-color:#000;
	display:none;
	 '>&nbsp;
</div>
<!--END-->
<script>
    $( document ).ready(function() {
        
         $('#dim_wrapper').click(function(e) {
			$('#dim_wrapper').toggle();
			$("#dim_wrapper").animate({
				'opacity':1.0
			}, 0 );
			
			hide_popup("my_popup");
		});
                
        $('#submit').click(function(e) {
            
            
            
            for(var i=1;i<10;i++)
                document.getElementById("req"+i).innerHTML = "";
            
            if($("#mycat").val() == "")
            {
                document.getElementById("req1").innerHTML = "*Required";
                e.preventDefault();
                $("#mycat").focus();
            }
            
            else if($("#name").val() == "")
            {
                document.getElementById("req2").innerHTML = "*Required";
                e.preventDefault();
                $("#name").focus();
            }
            else if($("#name").val().length <=3)
            {
                document.getElementById("req2").innerHTML = "*Must be longer than 3";
                e.preventDefault();
                $("#name").focus();
            }
            else if(!isNaN($("#name").val()))
            {
                document.getElementById("req2").innerHTML = "*Must be alphanumeric";
                e.preventDefault();
                $("#name").focus();
            }
            
           else if($("#price").val() == "")
            {
                document.getElementById("req3").innerHTML = "*Required";
                e.preventDefault();
                $("#price").focus();
            }
            else if(isNaN($("#price").val()))
            {
                document.getElementById("req3").innerHTML = "*Must be a number";
                e.preventDefault();
                $("#price").focus();
            }
            else if($("#amount").val() == "")
            {
                document.getElementById("req4").innerHTML = "*Required";
                e.preventDefault();
                $("#amount").focus();
            }
            else if(isNaN($("#amount").val()))
            {
                document.getElementById("req4").innerHTML = "*Must be a number";
                e.preventDefault();
                $("#amount").focus();
            }
            else if($("#brand").val() == "")
            {
                document.getElementById("req5").innerHTML = "*Required";
                e.preventDefault();
                $("#brand").focus();
            }
            else if($("#desc").val() == "")
            {
                document.getElementById("req6").innerHTML = "*Required";
                e.preventDefault();
                    $("#desc").focus();
            }
            
                else if(document.getElementsByName("size1")[0].checked ==false && document.getElementsByName("size2")[0].checked ==false && document.getElementsByName("size3")[0].checked ==false )
                {
                    document.getElementById("req7").innerHTML = "*Required one atleast";
                    e.preventDefault();
                        document.getElementsByName("size1")[0].focus();
                }
          else  if($("#color").val() == "")
            {
                document.getElementById("req8").innerHTML = "*Required";
                e.preventDefault();
                $("#color").focus();
            }
            else if($("#color").val().length <3)
            {
                document.getElementById("req8").innerHTML = "*Must be longer than 2";
                e.preventDefault();
                $("#color").focus();
            }
            else if(!isNaN($("#color").val()))
            {
                document.getElementById("req8").innerHTML = "*Must be alphabetic";
                e.preventDefault();
                $("#color").focus();
            }
            
            else if($("#image").val() == "")
            {
                document.getElementById("req9").innerHTML = "*Required";
                e.preventDefault();
                $("#image").focus();
            }
             else{
                 
                 switch($("#image").val().substring($("#image").val().lastIndexOf('.') + 1).toLowerCase()){
        case 'gif': case 'jpg': case 'png':
            break;
        default:
            e.preventDefault();
            $("#image").val("");
           document.getElementById("req9").innerHTML = "*Not an image";
            break;
    }
    
             }
        
            
            
          });
});

function show_popup(id) {
			if (document.getElementById){
				obj = document.getElementById(id);
				if (obj.style.display == "none") {
					obj.style.display = "";
				}
			}
		}
		function hide_popup(id){
			if (document.getElementById){
				obj = document.getElementById(id);
				if (obj.style.display == ""){
					obj.style.display = "none";
				}
			}
		}

</script>
<jsp:include page="admin_footer.jsp" />